<%@page import="by.epam.communalservices.exception.JkhDaoException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="by.epam.communalservices.dao.jdbc.JdbcDaoUser"%>
<%@page import="by.epam.communalservices.entity.User"%>
<%@page import="by.epam.communalservices.utils.Login"%>
<%@page import="java.util.HashSet"%>
<%@page import="java.util.Set"%>
<%@page import="java.sql.Connection"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/connect" prefix="connect"%>
<%@ taglib tagdir="/WEB-INF/tags/templates" prefix="tmpl"%>
<%@ taglib tagdir="/WEB-INF/tags/table" prefix="table"%>
<tmpl:head>
	<link rel='shortcut icon' href=misc/favicon.html type='image/x-icon' />
	<script language="JavaScript" type="text/javascript" src="scripts/functions.js" ></script>
	<link href='styles/common.css' rel='stylesheet' type='text/css' />
	<link href='styles/header.css' rel='stylesheet' type='text/css' />
	<link href='styles/table.css' rel='stylesheet' type='text/css' />
</tmpl:head>

<connect:close/>
<connect:isset test="${param.submit}" />

<%


    request.setAttribute("message", "");
    if (request.getParameter("submit") != null) {
        if (request.getParameter("name").length() == 0) {
            request.setAttribute("message", "Не введено имя");
        } else if (request.getParameter("pass").length() == 0) {
            request.setAttribute("message", "Не введен пароль");
        } else {
            Connection con = (Connection) request.getSession().getAttribute("currentConnection");

            Login login = new Login(con);
            User currentUser = null;
            Set<String> accsess = null;
            try {
                currentUser = login.getUser(request.getParameter("name"), request.getParameter("pass"));
            } catch (Exception ex) {
				
            }

            if (currentUser == null) {
                request.setAttribute("message", "Неправильное имя или пароль");
            } else {
                //User currentUser = new User(1l, request.getParameter("name"), request.getParameter("pass"), 1l);
                accsess = login.getRole();

                request.getSession().setAttribute("currentUser", currentUser);
                accsess.add("hello");
                accsess.add("index");
                request.getSession().setAttribute("currentAccsess", accsess);
                request.setAttribute("message", "Logined");
                request.setAttribute("logined", true);
            }

        }
    }

    if (request.getParameter("logout") != null) {
        request.getSession().removeAttribute("currentUser");
        request.getSession().removeAttribute("currentAccsess");
        request.getSession().removeAttribute("currentConnection");
        request.setAttribute("logined", false);
    }

%>
<tmpl:common> 
	<c:choose>
		<c:when test="${requestScope.logined}">
			<%-- <c:redirect url="/index" /> --%>
			<c:import url="/WEB-INF/pages/index.jsp" />
		</c:when>
		<c:otherwise>
			<c:if test="${requestScope.message ne ''}">
				<p style='color: red; border: 1px solid red;'>
					<c:out value="${message}" />
				</p>
			</c:if>
			<form method='POST' action='login'>
				<table:body style='width:40%;'>
				<table:title span="4">Логин</table:title>
				<tr id=n class=t>
					<td colspan=4 id=l>Для входа в систему необходимо ввести в соответсвующие поля имя пользователя и пароль.</td>
				</tr>
				<tr id=c class=t>
					<td colspan=2 id=l>Имя пользователя:</td>
					<td colspan=2 id=l>Пароль:</td>
				</tr>
				<tr id=n class=t>
					<td colspan=2 id=l><input name='name' value='${param.name}'></td>
					<td colspan=2 id=l><input name='pass' value='' type='password'></td>
				</tr>
				<table:title span="4" down="yes"><input name='submit' value='ok'	type='submit'></table:title>
				</table:body>
			</form>
		</c:otherwise>
	</c:choose>
</tmpl:common>